$(function() {
	question_upload_file.init();
});

var question_upload_file = {
		init: function init(){
//			this.drawModal();
			this.prepareUploadify();
			this.prepareDialog();
			this.bindDisplayFile();
		},
		
		drawModal : function drawModal(){
//			$("#question-add-form").append("<div class=\"modal\" id=\"add-question-file-dialog\"><div title=\"图片上传工具\" class=\"modal-dialog modal-lg\"><div class=\"modal-content\"><form><div class=\"form-line file-destination\"><span class=\"form-label\">添加至：</span><label></label><input type=\"hidden\" value=\"\"/></div><div class=\"form-line add-update-quetstionfile\"><span class=\"form-label\">上传图片：</span><div id=\"div-file-list\"></div><div class=\"form-line\" id=\"uploadify\"></div><span class=\"form-message\">请上传png、jpg图片文件，且不能大于100KB。为了使得图片显示正常，请上传的图片长宽比例为2:1</span></div></form></div></div></div>");
		},
		
		prepareDialog : function prepareDialog() {
			$("#question-add-form").on("click",".add-file",function() {
				$(".fade-file").modal({backdrop:true,keyboard:true});
				$("#file-name").empty();
				if($(this).hasClass("add-content-file")){
					$(".file-destination label").text("试题内容");
					$(".file-destination input").val(-1);
				}else if($(this).hasClass("add-opt-file")){
					$(".file-destination label").text("试题选项 ");
					var this_index = $(".add-opt-file").index($(this));
					$(".file-destination label").append(String.fromCharCode(65 + this_index));
					$(".file-destination input").val(this_index);
				}
			});
		},
		prepareUploadify : function prepareUploadify(){
			$("#uploadify-file").uploadify({
				'debug'	 : false,
				'buttonText'	: '点击上传',
				'buttonCursor'	: 'pointer',
				'uploader'	 : document.getElementsByTagName('base')[0].href + 'admin/upload-uploadify/',
				'swf'	 : document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/uploadify.swf',
				'multi'	 : false,
				'auto'	 : true,
				'height'	 : '26',
				'width'	 : '60',
				'requeueErrors'	: false,
				'fileSizeLimit'	: '184320', // expects input in kb
				'cancelImage'	: document.getElementsByTagName('base')[0].href + 'resources/js/uploadify/cancel.png',
				
				overrideEvents:['onSelectError','onDialogClose'],
				onUploadProgress: function() {
//					$('#loader').show();
				},
				onUploadComplete: function(file) {
//					$('#div-file-list').html('<a id=\'file-name\'>' + file.name + '</a>');
//					$('#loader').fadeOut(100);
					//alert(file);
					//console.log(file);
					$('#maincontent').load(location.href+' #maincontent > *');
					$(".fade-file").modal('hide');
				},
				onUploadSuccess : function(file, data, response) {
//			            alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
			        	//alert(data);
			        	var fileurl = data.split("|")[0];
			        	var filename = data.split("|")[1];
			        	var destination  = $(".file-destination input").val();
			        	if(destination == -1){
//			        		var textareaval = $(".question-content textarea").val();
//			        		$(".question-content textarea").val( textareaval + "<img class=\"question-content-file\" src=\"" +  fileurl + "\">");
			        		
			        		var displayFile = $(".question-content").find(".diaplay-file");
			        		if(displayFile.length  == 0){
			        			$(".question-content textarea").after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-content-file\" target=\"_blank\" data-url=\"" + fileurl + "\">"+file.name+"</a>");
			        		}else{
			        			displayFile.attr("href", "..\\"+fileurl);
			        			displayFile.attr("data-url", fileurl);
			        			displayFile.setText(file.name);
			        		}
			        		
			        	}else{
			        		var thisopt =  $($(".add-opt-item")[destination]);
			        		var displayFile = thisopt.find(".diaplay-file");
			        		
			        		if(displayFile.length  == 0){
			        			thisopt.find("input.form-question-opt-item").after("<a href=\"..\\" + fileurl + "\" class=\"diaplay-file display-opt-file\" target=\"_blank\" data-url=\"" + fileurl + "\">下载附件</a>");
			        		}else{
			        			displayFile.attr("href", "..\\"+fileurl);
			        			displayFile.attr("data-url", fileurl);
			        			displayFile.setText(file.name);
			        		}
			        		
//			        		var optval = thisopt.val();
//			        		thisopt.val(optval + "<img class=\"question-opt-file\" src=\"" +  fileurl + "\">");
			        	}
			        	
			        	/*$("#add-question-file-dialog").dialog( "close" );*/
				},
				onSelectError: function(file,errorCode,errorMsg) {
					//alert(errorCode+" "+errorMsg);
					if(errorCode==-110){
						util.error("只能上传180M以下的文件。");
						return false;
					}
				},
				onUploadError: function(file,errorCode,errorMsg, errorString) {
					util.error(errorMsg);
				}
			});
		},
		
		bindDisplayFile : function bindDisplayFile(){
			$("#bk-conent-question-content").delegate(".diaplay-file","click",function() {
				window.open(location.protocol + "//" + location.host + "/" + $(this).attr("href"));
				e.preventDefault();
			});
		}
		
};